<?php

/**
 * 模型对应数据库表 "web_currency".
 *
 * 以下是可用表web_currency的栏位:
 * @property integer $id
 * @property string $currency_name
 * @property string $currency_code
 * @property string $currency_tag
 * @property string $currency_rate
 */
class WebCurrency extends CActiveRecord {

    /**
     * 返回指定的基于“ar”的静态模型类.
     * @return WebCurrency the static model class
     * @author nirsh
     * @example
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 返回数据库表的名.
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function tableName() {
        return 'web_currency';
    }

    /**
     * 返回验证规则
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function rules() {
        return array(
            array('currency_name, currency_tag,currency_code', 'required'),
            array('currency_name, currency_tag', 'length', 'max' => 255),
            array('currency_code', 'length', 'max' => 5),
            array('currency_rate', 'length', 'max' => 10),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, currency_name, currency_code, currency_tag, currency_rate', 'safe', 'on' => 'search'),
        );
    }

    /**
     * 返回关联规则数组。
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * 通过ID获取名称
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function getNameById($id, $nameField) {
        $res = $this->findByPk($id);
        return $res[$nameField] ? $res[$nameField] : '';
    }

    /**
     * 返回属性标签(名称= >标签)数组
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'currency_name' => '货币名称',
            'currency_code' => '货币代码',
            'currency_tag' => '货币符号',
            'currency_rate' => '汇率(以美元为基础)',
            'update_time' => '最后修改时间'
        );
    }

    /**
     * 设置查询相关条件
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.
        $criteria = new CDbCriteria;
        $criteria->compare('id', $this->id);
        $criteria->compare('currency_name', $this->currency_name, true);
        $criteria->compare('currency_code', $this->currency_code, true);
        $criteria->compare('currency_tag', $this->currency_tag, true);
        $criteria->compare('currency_rate', $this->currency_rate, true);
        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                ));
    }

    /**
     * 生成输入框
     * @author flwof
     * @param type $name
     * @param type $id
     * @param type $val 
     */
    public static function createInput($name, $id, $val) {
        echo CHtml::textField("{$name}_{$id}", $val, array('rel-id' => $id, 'rel-field' => $name, 'onchange' => "updateField(this)"));
    }
    
    /**
      +-----------------------------------------------------
     * 说明货币兑换 
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function changeCurrency($price){
        $cModel = $this->getMoneyObj();
        return array('code' => $cModel->currency_code, 'tag'=>$cModel->currency_tag,'price'=>round($price*$cModel->currency_rate,2));
    }
    /**
      +-----------------------------------------------------
     * 说明 获取货币
     * @param 
     * @return object
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getMoneyObj(){
        $cookie = Yii::app()->request->getCookies();
        $pk =  isset($cookie['siteCurrency'])?$cookie['siteCurrency']->value:'USD';
        return webCurrency::model()->find("currency_code='".$pk."'");
    }

}